<?php
/**
* @package mod_rokvirtuemart_categories
* @copyright	Copyright (C) 2009 RocketTheme. All rights reserved.
* @license		http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* RokVirtuemart Product Categories is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*
*/

defined('_JEXEC') or die('Restricted access');

global $mosConfig_live_site, $mainframe, $current_cat_id;

// Load the virtuemart main parse code
if( JPATH_SITE . '/components/com_virtuemart/virtuemart_parser.php' ) {
	require_once( JPATH_SITE.'/components/com_virtuemart/virtuemart_parser.php' );
} 

require_once(CLASSPATH . 'ps_product_category.php');

$uri =& JURI::getInstance($_SERVER['REQUEST_URI']);
$query = $uri->getQuery(true);
$current_cat_id = (count($query) && isset($query['category_id'])) ? intval($query['category_id']) : false;

class RokVMCategoryTree {
	function traverse_tree_down(&$category_html, $category_id = '0', $level = '0') {
		static $ibg = -1;
		global $mosConfig_live_site, $sess, $current_cat_id;
		$db = new ps_DB();
		$level++;

		$query = "SELECT category_name, category_id, category_child_id "
		. "FROM #__{vm}_category as a, #__{vm}_category_xref as b "
		. "WHERE a.category_publish='Y' AND "
		. " b.category_parent_id='$category_id' AND a.category_id=b.category_child_id "
		. "ORDER BY category_parent_id, list_order, category_name ASC";

		$db->query($query);

		while ($db->next_record()) {
			$ibg++;
			
			$tree_id = ($ibg == 0) ? 1 : $ibg;
			$item_id = '&Itemid=' . $sess->getShopItemid();
			$category_id = $db->f("category_id", false);
			$category_child_id = $db->f("category_child_id");
			$category_name = $db->f("category_name", false);
			$link = sefRelToAbs('index.php?option=com_virtuemart&page=shop.browse&category_id=' . $category_id . $item_id . "&TreeId=$tree_id");
			$hasChilds = ps_product_category::has_childs($category_id);
			$active = ($current_cat_id == $category_id) ? ' active ' : '';
			$current = ($current_cat_id == $category_id) ? ' id="current" ' : '';

			$category_html .= "<li " . $current . " class='item " . (($hasChilds) ? 'parent' : '') . $active . "'>";
			if ($hasChilds) $category_html .= "<div class='arrow'><span>+</span></div>";
			$category_html .= 	"<a href='$link'><span class='menuitem'>" . $category_name . "</span></a>";
			
			if ($hasChilds) {
				
				$category_html .= "<ul class='submenu'>";
				$this->traverse_tree_down($category_html, $category_child_id, $level);
				$category_html .= "</ul>";
			}

			$category_html .= "</li>";

		}
	}
	
	function _getJSVersion() {
		if (version_compare(JVERSION, '1.5', '>=') && version_compare(JVERSION, '1.6', '<')){
			if (JPluginHelper::isEnabled('system', 'mtupgrade')){
				return "-mt1.2";
			} else {
				return "";
			}
		} else {
			return "";
		}
	}
}

?>
